<?php

namespace App\Providers;

use App\Presenters\CommonPresenter;
use App\Presenters\PostPresenter;
use App\Repositories\CategoryRepository;
use App\Repositories\CommentRepository;
use App\Repositories\PostRepository;
use App\Repositories\TagRepository;
use app\services\CaptchaService;
use App\services\OrderService;
use App\services\ThirdApiService;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Predis\Client;

class SomeServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.er
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    protected $defer = true;

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('HttpClient',function($app){
            return new \GuzzleHttp\Client();
        });
        $this->mapRedisSingleton();
        $this->mapRepositorySingleton();
        $this->mapPresenterSingleton();
        $this->mapServiceSingleton();

    }

    protected function mapRedisSingleton()
    {
        $this->app->singleton('redis.default.client', function () {
            return new Client(config('database.redis.default'));
        });
        $this->app->singleton('redis.client', function ($app) {
            /**
             * @var $app Application
             */
            return new Client($app->make('config')->get('database.redis.redis'));
//            return new Client($app['config']['database']['redis']['redis']);
//            return new Client(config('database.redis.redis'));

        });
        $this->app->singleton('redis.broadcast.client', function () {
            return new Client(config('database.redis.broadcast'));
        });
        $this->app->singleton('redis.job.client', function () {
            return new Client(config('database.redis.job'));
        });
    }

    protected function mapRepositorySingleton()
    {
        $this->app->singleton('post.repository', function ($app){
            return new PostRepository($app);
        });
        $this->app->singleton('category.repository', function($app){
            return new CategoryRepository($app);
        });
        $this->app->singleton('comment.repository', function($app){
            return new CommentRepository($app);
        });
        $this->app->singleton('tag.repository', function($app){
            return new TagRepository($app);
        });
    }

    private function mapPresenterSingleton()
    {
        $this->app->singleton('post.presenter', function($app){
            return new PostPresenter();
        });
        $this->app->singleton('common.presenter', function($app){
            return new CommonPresenter();
        });
    }

    private function mapServiceSingleton()
    {
        $this->app->singleton('captcha.service',function($app){
            return new CaptchaService();
        });
        $this->app->singleton('order.service',function(){
            return new OrderService();
        });
        $this->app->singleton('thirdApi.service',function($app){
            return new ThirdApiService(new \GuzzleHttp\Client());
        });
    }
}
